import React from 'react'
import {Switch,Route,Redirect} from "react-router-dom"
import  {routerItem} from "./routerConfig"
interface Iprops {
    routes:routerItem[]
}
export default function routerView({routes}:Iprops) {
     let routeArr = routes.filter((item:routerItem)=>item.component)
     let redirectArr = routes.filter((item:routerItem)=>item.redirect)
    return (
        <Switch>
            {
                routeArr.map((item:routerItem)=><Route key={item.path} path={item.path} render={props=>{
                    if(item.children){
                        return <item.component routes={item.children} {...props}/>
                    }
                    return <item.component {...props}/>
                }}></Route>)
            }
            {
                redirectArr.map((item:routerItem)=>{
                    return <Redirect key={item.path} from={item.path} to={item.redirect} />
                })
            }
        </Switch>
    )
}
